using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditor.SceneManagement;

public class NovelExtractorSceneSetup
{
    [MenuItem("Novel Extractor/Setup Scene")]
    public static void SetupScene()
    {
        // Create a new scene
        EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);

        // Create UI GameObject
        GameObject uiObject = new GameObject("FileUploadUI");
        
        // Add UIDocument component
        UIDocument uiDocument = uiObject.AddComponent<UIDocument>();
        
        // Set the UXML asset
        string uxmlPath = "Assets/UI/FileUploadUI.uxml";
        VisualTreeAsset visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath);
        uiDocument.visualTreeAsset = visualTreeAsset;

        // Set the USS asset
        string ussPath = "Assets/UI/NovelExtractorUI.uss";
        ThemeStyleSheet themeStyleSheet = AssetDatabase.LoadAssetAtPath<ThemeStyleSheet>(ussPath);
        
        // 创建一个新的PanelSettings资产
        var panelSettings = ScriptableObject.CreateInstance<PanelSettings>();
        if (themeStyleSheet != null)
        {
            panelSettings.themeStyleSheet = themeStyleSheet;
        }
        else
        {
            Debug.LogWarning("无法加载ThemeStyleSheet。请确保USS文件兼容或考虑使用其他方式应用样式。");
        }
        
        // 将PanelSettings应用到UIDocument
        uiDocument.panelSettings = panelSettings;

        // Add NovelExtractorUI component
        uiObject.AddComponent<FileUploadUI>();

        // Save the scene
        string scenePath = "Assets/Scenes/NovelExtractorScene.unity";
        EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), scenePath, true);

        Debug.Log("Novel Extractor scene setup complete. Scene saved at: " + scenePath);
    }
}